<?php

declare(strict_types=1);

namespace app\user\service;

use sheep\exception\SheepException;
use app\user\model\User as UserModel;

class User
{
    /**
     * @name 追加消费金额
     * @param  int|object   $user       会员对象或会员ID
     * @param  float        $amount      变更金额
     * @return boolean
     */
    public static function consume($user, $amount)
    {
        // 判断金额
        if ($amount == 0) {
            return false;
        }

        // 判断用户
        if (is_numeric($user)) {
            $user = UserModel::getById($user);
        }
        if (!$user) {
            throw new SheepException('未找到用户');
        }


        // 更新会员余额信息
        $user->inc('total_consume', $amount)->update();

        return true;
    }
}
